//
//  AppUtils.m
//  YouthOlympic2014
//
//  Created by Li Yuhang on 14-7-1.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "AppUtils.h"

@implementation AppUtils
/*
 中文：   CHI
 英文：   ENG
 法文：   FR
 */
+ (NSString*)getLan{
    NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSString* systemLanguage = @"";
    if ([langID isEqualToString:@"zh-Hans"]) {
        systemLanguage = @"CHI";
    }else if ([langID isEqualToString:@"fr"]) {
        systemLanguage = @"FR";
    }else{
        systemLanguage = @"ENG";
    }
    //NSLog(@"systemLanguage is %@",langID);
    return systemLanguage;
}
+ (NSDateComponents*) getDateComponetsFromNSDate:(NSDate *)date{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *curDate = date;
    NSInteger unitFlags =NSDayCalendarUnit;
    NSDateComponents *comps =  [calendar components:unitFlags fromDate:curDate];
    return comps;
}

+ (NSInteger)getCurDay{
    NSInteger today = [[AppUtils getDateComponetsFromNSDate:[NSDate date]] day];
    return today;
}

+ (NSInteger)getCurDayInYogRange{
    
    NSInteger today = [AppUtils getCurDay];
    //NSInteger today = 27;
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    if([dateStr integerValue]< 20140814){
        today = 14;
    }else if([dateStr integerValue]> 20140828){
        today = 28;
    }
    //NSLog(@"dateStr %d",[dateStr integerValue]);
    return today;
}

+ (NSString*)getCurDayDescriptionInYogRange{
    
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyyMMdd"];
    
    NSDateFormatter* dOut = [[NSDateFormatter alloc] init];
    [dOut setDateFormat:@"yyyy-MM-dd"];
    //
    NSString* inputDate =[df stringFromDate:[NSDate date]];
    //
    NSString* outputDate =[dOut stringFromDate:[NSDate date]];
    
    
    return [NSString stringWithFormat:@"%@", ([inputDate compare:@"20140814"] ==NSOrderedAscending ) ? @"2014-08-14" :(([inputDate compare:@"20140828"] == NSOrderedDescending) ? outputDate : @"2014-08-28")];
}

+ (NSString*)getYMDDateStrInYogRangeFromNSDate:(NSDate*) date{
    
    
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyyMMdd"];
    //
    NSString* inputDate =[df stringFromDate:date];
    //
    return [NSString stringWithFormat:@"%@", ([inputDate compare:@"20140814"] ==NSOrderedAscending ) ? @"20140814" :(([inputDate compare:@"20140828"] == NSOrderedDescending) ? inputDate : @"20140828")];
}

+ (NSString*)getCurYMDDateStrInYogRange{
    return [self getYMDDateStrInYogRangeFromNSDate:[NSDate date]];
}

+ (NSDictionary*)parseApiTimeToDictionary: (NSString*) timeStr{
    //20130816133000
    NSMutableDictionary* result = [[NSMutableDictionary alloc]init];
    result[@"year"] = [timeStr substringWithRange:NSMakeRange(0, 4)];
    result[@"month"] = [timeStr substringWithRange:NSMakeRange(4, 2)];
    result[@"day"] = [timeStr substringWithRange:NSMakeRange(6, 2)];
    result[@"hour"] = [timeStr substringWithRange:NSMakeRange(8, 2)];
    result[@"minute"] = [timeStr substringWithRange:NSMakeRange(10, 2)];
    result[@"second"] = [timeStr substringWithRange:NSMakeRange(12, 2)];
    //
    return [NSDictionary dictionaryWithDictionary:result];
}



+ (NSString *)replaceUnicodeDic:(NSDictionary *)dic
{
    NSString *tempStr1 = [[dic description] stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
                                                           mutabilityOption:NSPropertyListImmutable
                                                                     format:NULL
                                                           errorDescription:NULL];
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}

+ (UIImage*) getPlaceIcon:(NSString *)placePicTag
{
    NSDictionary* placeDict = @{
                            @"FSB":@"FSB",
                            @"HCS":@"HCS",
                            @"IEC":@"IEC",
                            @"JNC":@"JNC",
                            @"NSI":@"NSI",
                            @"OSC":@"OSC",
                            @"OSP":@"OSP",
                            @"WTC":@"WTC",
                            @"OTH":@"OTH",
                            @"LJG":@"LJG",
                            @"LSP":@"LSP",
                            @"ZSG":@"ZSG",
                            @"XRC":@"XRC",
                            @"XWT":@"XWT",
                            @"JLS":@"JLS",
                            @"YSL":@"YSL",
                            @"XZE":@"XZE"
                                };
    
    return [UIImage imageNamed: placeDict[placePicTag]];
}

+ (NSString*) getTodayDescriptionForListHeaderView
{
     NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    return [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"今天", nil),[df stringFromDate:[NSDate date]]];
}
@end
